package lg.minecraft.plugin.pokedex.task;

import lg.minecraft.plugin.pokedex.LGPokedex;
import lg.minecraft.plugin.pokedex.manager.PokedexPlayerManger;
import lg.minecraft.plugin.pokedex.storage.DataStorage;
import lg.minecraft.plugin.pokedex.storage.YMALStorage;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;

/**
 * @author langle__
 * @version 1.0
 */
public class PlayerDataTask extends BukkitRunnable {

    private BukkitTask task;

    public void start() {
        if (task != null) task.cancel();
        task = runTaskTimerAsynchronously(LGPokedex.getInstance(), 20 * 60, 20 * 60);
    }

    public void stop() {
        if (task != null) {
            task.cancel();
            task = null;
        }
    }

    @Override
    public void run() {
        PokedexPlayerManger pokedexPlayerManger = LGPokedex.getPokedexPlayerManger();
        if (pokedexPlayerManger.getCurrentCacheSize() == 0) return;
        DataStorage dataStorage = pokedexPlayerManger.getDataStorage();
        if (dataStorage instanceof YMALStorage) {
            ((YMALStorage) dataStorage).saveAllData();
        }
    }

}
